/**
 *  This class is just POC for polynomial class from commons-math
 */


package main;


import org.apache.commons.math3.analysis.integration.SimpsonIntegrator;
import org.apache.commons.math3.analysis.integration.UnivariateIntegrator;
import org.apache.commons.math3.analysis.polynomials.*;


/**
 * @author Vivek
 *
 */
public class ExperimentPoly {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		System.out.println("Enter the name :");
		
		PolynomialFunction pfa[] = new PolynomialFunction[3]; 
				
				
		pfa[0] = new PolynomialFunction(new double[]{0,0,1,0}); 
		pfa[1] = (PolynomialFunction) pfa[0].derivative();
		pfa[2] = (PolynomialFunction) pfa[1].derivative();
		
//		UnivariateIntegrator asd = new  SimpsonIntegrator();
		
		System.out.println(pfa[0].toString());

//		System.out.println(asd.getIterations());

		System.out.println(getPolynomialArcLength(pfa[0],-1,1,200));
		
	}

	/**
	 * This function is used to calculate calculate the arc length of an
	 * polynomial. Specifically this function creates an array of points on
	 * curve at regular intervals (that is passed as parameters)
	 * 
	 * Its makes a call to getPolynomialLength to calculate the sum length for
	 * all the points.
	 * 
	 * @param univariate
	 *            Polynomial function whose arc length needs to be calculates
	 * @param start
	 *            X coordinate for the start point.
	 * @param end
	 *            X coordinates for end point.
	 * @param interval
	 *            total number of interval to be considered for calculation of
	 *            arc length
	 * @return
	 */
	private static double getPolynomialArcLength(PolynomialFunction univariate,
			double start, double end, int interval) {
		double step = (double) (end - start) / interval;
		double[][] coord = new double[1 + interval][2];

		for (int counter = 0; counter <= interval; counter++) {
			coord[counter][0] = start + counter * step;
			coord[counter][1] = univariate.value(coord[counter][0]);

		}
		// System.out.println(Arrays.toString(coord[0]));
		// System.out.println(Arrays.toString(coord[1]));
		// System.out.println("Lenght"+coord.length+" Step:"+step);
		return getPolynomialLength(coord);
	}

	public static double getPolynomialLength(double[][] xy) {
		double length[] = new double[xy.length - 1];

		for (int n = 1; n < xy.length; n++) {
			length[n - 1] = Math.sqrt(Math.pow((xy[n - 1][0] - xy[n][0]), 2)
					+ Math.pow((xy[n - 1][1] - xy[n][1]), 2));
			// System.out.println("HEre :"+length[n-1]);
		}
		double sum = 0;
		for (double i : length) {
			sum += i;
		}

		return sum;
	}

}
